// 修改密码相关路由配置

const Router = require('@koa/router');
const mongoose = require('mongoose');
const { verify, getToken } = require('../../helpers/token');
const UserModel = mongoose.model('User');

const router = new Router({
  prefix: '/profile',
});

// 角色列表接口
router.post('/updatePassword', async (ctx) => {
  console.log('--> 调用接口 post /profile/updatePassword');
  const { oldPassword, newPassword } = ctx.request.body;
  const { _id } = await verify(getToken(ctx));
  const user = await UserModel.findOne({ _id }).exec();
  if (user.password !== oldPassword) {
    ctx.body = {
      msg: '原密码不正确',
      code: 0,
      data: null,
    };
    return;
  }
  user.password = newPassword;
  await user.save();
  ctx.body = {
    msg: '修改密码成功',
    code: 1,
    data: null,
  };
});
module.exports = router;
